package com.thread;

import org.junit.Test;

import javax.sound.midi.Soundbank;
import java.util.concurrent.TimeUnit;

/**
 * Copyright (C) @2021 GuangDong Eshore Technology Co. Ltd
 *
 * @author: mayu
 * @version: 1.0
 * @date: 2021/4/1
 * @time: 13:13
 * @description: 结束线程的2种方式
 */
public class ThreadTerminated {
    //方式1
    private static volatile boolean running = true;

    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            Long i = 0L;
            while (running) {
                i++;
                /*try {
                    Thread.sleep(Integer.MAX_VALUE);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }*/
            }
            System.out.println("i=" + i);// 169128203  178896892
        });

        t.start();

        //Thread.sleep(1000);

        TimeUnit.SECONDS.sleep(1);

        running = false;

    }

    //方式2
    @Test
    public void test() throws InterruptedException {
        Thread t = new Thread(() -> {
            Long i = 0L;
            while (!Thread.interrupted()) {
                i++;
                /*try {
                    Thread.sleep(Integer.MAX_VALUE);
                } catch (InterruptedException e) {
                    System.out.println("i= " + i);
                    //e.printStackTrace();
                }*/
            }
            System.out.println("i= " + i);
        });

        t.start();

        //Thread.sleep(1000);

        TimeUnit.SECONDS.sleep(1);

        t.interrupt();
    }

}
